package com.xgimi.defaultapp.adpter;

import java.util.ArrayList;
import java.util.List;

import com.xgimi.defaultapp.R;
import com.xgimi.defaultapp.utils.CustomApplicationHelper;
import com.xgimi.defaultapp.view.DefualtAppItemView;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class DefaultApater extends BaseAdapter {

	private Context mContext;
	List<ResolveInfo> mDefaultInfoList = new ArrayList<ResolveInfo>();
	PackageManager pm;
	CustomApplicationHelper mCusApp;

	public DefaultApater(Context context, CustomApplicationHelper cusApp,
			List<ResolveInfo> defaultInfoList) {
		this.mContext = context;
		this.mDefaultInfoList = defaultInfoList;
		this.mCusApp = cusApp;
		this.pm = mContext.getPackageManager(); // 获得PackageManager对象
	}

	@Override
	public int getCount() {
		return mDefaultInfoList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DefaultViewHolder defaultViewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_listview, null);
			defaultViewHolder = new DefaultViewHolder(convertView);
			convertView.setTag(defaultViewHolder);
		} else {
			defaultViewHolder = (DefaultViewHolder) convertView.getTag();
		}
		//
		ResolveInfo info = mDefaultInfoList.get(position);
		if (info != null) {
			defaultViewHolder.mDefualtAppItemView.setAppName(info.loadLabel(pm).toString());
			defaultViewHolder.mDefualtAppItemView.setAppIcon(info.loadIcon(pm));
			boolean isDefualt = mCusApp.isDefaultApp(info.activityInfo.packageName);
			defaultViewHolder.mDefualtAppItemView.setDefaultApp(isDefualt);
		}
		return convertView;
	}

	class DefaultViewHolder {
		public DefualtAppItemView mDefualtAppItemView;

		public DefaultViewHolder(View itemView) {
			mDefualtAppItemView = (DefualtAppItemView) itemView.findViewById(R.id.defaultAppItemView);
		}
	}

}
